public class Solution876 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode middleNode(ListNode head) {
        ListNode cur=head;
        int c=0;
        while (cur!=null){
            c++;
            cur=cur.next;
        }
        for (int i=0;i<c/2;i++){
            head=head.next;
        }
        return head;
    }

    public static void main(String[] args) {
        System.out.println(new Solution876().middleNode(new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5)))))).val);
    }
}
